package com.tom.joke.permission;

import android.app.Activity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import java.util.List;

/**
 * PermissionHelper.requestPermission(this, CALL_PHONE_REQUEST_CODE, new String[]{Manifest.permission.CALL_PHONE});
 * PermissionHelper.with(this).requestCode(CALL_PHONE_REQUEST_CODE)
 *     .requestPermissions(Manifest.permission.CALL_PHONE).request();
 */
public class PermissionHelper {
    private Object mObject; // 是Activity或Fragment。
    private int mRequestCode;
    private String[] mRequestPermissions;

    private PermissionHelper(Object object) {
        this.mObject = object;
    }

    // 一、传递什么参数，1.Fragment/Activity, 2.请求码, 3.需要请求的权限 String[]
    public static void requestPermission(Activity activity, int requestCode, String[] permissions) {
        PermissionHelper.with(activity).requestCode(requestCode)
                .requestPermissions(permissions)
                .request();
    }

    public static void requestPermission(Fragment fragment, int requestCode, String[] permissions) {
        PermissionHelper.with(fragment).requestCode(requestCode)
                .requestPermissions(permissions)
                .request();
    }

    // 二.链式调用，传递参数。activity,fragment
    public static PermissionHelper with(Activity activity) {
        return new PermissionHelper(activity);
    }

    public static PermissionHelper with(Fragment fragment) {
        return new PermissionHelper(fragment);
    }

    public PermissionHelper requestCode(int requestCode) {
        this.mRequestCode = requestCode;
        return this;
    }

    public PermissionHelper requestPermissions(String... permissions) {
        this.mRequestPermissions = permissions;
        return this;
    }

    // 3.0 真正判断和发起权限的请求。
    public void request() {
        // 3.1 判断是否6.0+，如果不是就直接执行方法，通过反射执行方法
        if (!PermissionUtils.isOverMarshmallow()) {
            // 执行什么方法？ 只能通过采用注解的方式给方法打一个标记，然后通过反射去执行方法。直接执行方法。
            PermissionUtils.executeSuccessMethod(mObject, mRequestCode);
            return;
        }

        // 3.2 是大于6.0，先判断权限是否授予？
        List<String> deniedPermissions = PermissionUtils.getDeniedPermission(mObject, mRequestPermissions);
        // 3.2.1 授予了权限，就直接执行，通过反射执行。
        if (deniedPermissions.size() == 0) {
            // 所有权限都曾经授予了。直接执行方法。
            PermissionUtils.executeSuccessMethod(mObject, mRequestCode);
        } else {
            // 3.2.2 没有授予权限，就申请权限。
            // list 转数组。
            String[] permissions = deniedPermissions.toArray(new String[deniedPermissions.size()]);
            ActivityCompat.requestPermissions(PermissionUtils.getActivity(mObject),
                    permissions, mRequestCode);
        }
    }

    /**
     * 处理申请权限的回调 callback
     *
     * @param obj         Activity或Fragment
     * @param requestCode 请求码
     * @param permissions 请求的权限
     */
    public static void requestPermissionsResult(Object obj, int requestCode, String[] permissions) {
        // 再次获取没授权的权限
        List<String> deniedPermissions = PermissionUtils.getDeniedPermission(obj, permissions);
        if (deniedPermissions.size() == 0) {
            // 所有权限都同意了。直接执行方法。
            PermissionUtils.executeSuccessMethod(obj, requestCode);
        } else {
            // 申请的权限中，有用户不同意的
            PermissionUtils.executeFailMethod(obj, requestCode);
        }
    }
}

